package com.maker.LMMS.service;

import com.blade.mvc.http.Request;
import com.maker.LMMS.model.*;

/**
 * @author : zimu
 * @Date: 2018/8/6 07:51
 * @description :
 */
public class AdminImpl {
    /*
     * @description :后台登录
     * @param email
     * @param password
     * @param request
     * @return
     */
    public boolean login(String username, String password, Request request) {
        Admin users = new Admin();
        Md5 md5 = new Md5();
        request.session().attribute("admin_name", null);
        password = md5.getMd5(password);
        users.setA_username(username);
        users.setA_password(password);
        Admin findResult = users.find();
        if(findResult!=null){
            findResult.setA_password("");
            request.session().attribute("admin_name", findResult);
            return true;
        }
        else {
            return false;
        }
    }

    /**
     * @description: 后台退出登录
     * @param request
     */
    public void logout(Request request){
        request.session().remove("admin_name");
    }

    /**
     * @description 添加管理员
     * @param username
     * @param password
     * @param realname
     * @param phone
     * @param request
     */
    public void addRole(String username, String password, String realname, String phone, Request request) {
        Admin users = new Admin();
        Md5 md5 = new Md5();
        password = md5.getMd5(password);
        users.setA_realname(realname);
        users.setA_password(password);
        users.setA_username(username);
        users.setA_phone(phone);
        users.save();
    }

    /**
     * 编辑管理员
     * @param id
     * @param username
     * @param realname
     * @param phone
     * @param request
     */
    public void editRole(int id,String username, String realname, String phone, Request request) {
        Admin users = new Admin();
        users.setA_realname(realname);
        users.setA_username(username);
        users.setA_phone(phone);
        users.update("a_id",id);
    }

    /**
     * @description 更新资料
     * @param a_id
     * @param username
     * @param phone
     * @param request
     */
    public void updateInfo(int a_id,String username,String phone, Request request) {
        Admin admin = new Admin();
//        admin.setA_id(a_id);
//        Admin user=admin.find();
        admin.setA_username(username);
        admin.setA_phone(phone);
        admin.update("a_id",a_id);

        //更新session
        request.session().attribute("admin_name", null);
        Admin admin2 = new Admin();
        admin2.setA_id(a_id);
        admin2 = admin2.find();
        if(admin2!=null){
            admin2.setA_password("");
            request.session().attribute("admin_name", admin2);
        }
    }

    /**
     * @description 更新关于我们
     * @param content
     * @param request
     */
    public void updateAboutus(String content, Request request) {
        Aboutus aboutus = new Aboutus();
        aboutus.setAb_content(content);
        aboutus.update("ab_id", 1);
    }

    public String updatePwd(Integer id,String oldpwd,String pwd,Request request) {
        Admin admin = new Admin();
        Md5 md5 = new Md5();
        oldpwd = md5.getMd5(oldpwd);
        pwd = md5.getMd5(pwd);
        admin.setA_id(id);
        admin.setA_password(oldpwd);
        admin = admin.find();
        if (admin!=null)
        {
            admin.setA_password(pwd);
            admin.update("a_id",id);
            return "修改成功！";
        }
        else
        {
            return "原密码输入有误！";
        }
    }


    public void editHistory(int d_id, String title, String content, String address, String time, Request request) {
        Development development = new Development();
        development.setD_content(content);
        development.setD_place(address);
        development.setD_time(time);
        development.setD_title(title);
        development.update("d_id",d_id);
    }

    public void addHistory(String title, String content, String address, String time, Request request) {
        Development development = new Development();
        development.setD_content(content);
        development.setD_place(address);
        development.setD_time(time);
        development.setD_title(title);
        development.save();
    }

    public void editGroup(int g_id, String gname, int leader, String describe, Request request) {
        Group group = new Group();
        group.setG_gname(gname);
        group.setG_leader(leader);
        group.setG_describe(describe);
        group.update("g_id",g_id);
    }

    public void addGroup(String gname, int leader, String describe, Request request) {
        Group group = new Group();
        group.setG_gname(gname);
        group.setG_leader(leader);
        group.setG_describe(describe);
        group.save();
    }

    public boolean checkVcode(Request request,String vcode) {
        if (request.session().attribute("code").toString().toUpperCase().equals(vcode.toUpperCase())) {
            return true;
        }
        return false;
    }

}
